home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STDLIB.PAK / INR_PROD.CPP < prev    next >
Text File  |  1997-05-06  |  1KB  |  41 lines

  1.  #include <numeric>       // For inner_product.
  2.  #include <list>          // For list.
  3.  #include <vector>        // For vectors.
  4.  #include <functional>    // For plus and minus.
  5.  
  6.  using namespace std;
  7.  
  8.  int main ()
  9.  {
  10.    //
  11.    // Initialize a list and an int using arrays of ints.
  12.    //
  13.    int a1[3] = {6, -3, -2};
  14.    int a2[3] = {-2, -3, -2};
  15.  
  16.    list<int>   l(a1+0, a1+3);
  17.    vector<int> v(a2+0, a2+3);
  18.    //
  19.    // Calculate the inner product of the two sets of values.
  20.    //
  21.    int inner_prod = inner_product(l.begin(), l.end(), v.begin(), 0);
  22.    //
  23.    // Calculate a wacky inner product using the same values.
  24.    //
  25.    int wacky = inner_product(l.begin(), l.end(), v.begin(), 0,
  26.                              plus<int>(), minus<int>());
  27.    //
  28.    // Print the output.
  29.    //
  30.    cout << "For the two sets of numbers: " << endl << "     ";
  31.    copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
  32.    cout << endl << " and  ";
  33.    copy(l.begin(),l.end(),ostream_iterator<int>(cout," "));
  34.  
  35.    cout << "," << endl << endl;
  36.    cout << "The inner product is: " << inner_prod << endl;
  37.    cout << "The wacky result is: " << wacky << endl;
  38.  
  39.    return 0;
  40.  }
  41.